| Conditions | 23 |
| Total Lines | 186 |
| Code Lines | 123 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like ODFViewerPlugin.js ➔ ODFViewerPlugin often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | /** |
||
| 28 | function ODFViewerPlugin() { |
||
| 29 | "use strict"; |
||
| 30 | |||
| 31 | function init( callback ) { |
||
| 32 | var lib = document.createElement('script'), |
||
| 33 | pluginCSS; |
||
|
|
|||
| 34 | |||
| 35 | lib.async = false; |
||
| 36 | lib.src = './webodf.js'; |
||
| 37 | lib.type = 'text/javascript'; |
||
| 38 | lib.onload = function () { |
||
| 39 | runtime.loadClass('gui.HyperlinkClickHandler'); |
||
| 40 | runtime.loadClass('odf.OdfCanvas'); |
||
| 41 | runtime.loadClass('ops.Session'); |
||
| 42 | runtime.loadClass('gui.CaretManager'); |
||
| 43 | runtime.loadClass("gui.HyperlinkTooltipView"); |
||
| 44 | runtime.loadClass('gui.SessionController'); |
||
| 45 | runtime.loadClass('gui.SvgSelectionView'); |
||
| 46 | runtime.loadClass('gui.SelectionViewManager'); |
||
| 47 | runtime.loadClass('gui.ShadowCursor'); |
||
| 48 | runtime.loadClass('gui.SessionView'); |
||
| 49 | |||
| 50 | callback(); |
||
| 51 | }; |
||
| 52 | |||
| 53 | document.head.appendChild(lib); |
||
| 54 | |||
| 55 | // pluginCSS = /**@type{!HTMLStyleElement}*/(document.createElementNS(document.head.namespaceURI, 'style')); |
||
| 56 | // pluginCSS.setAttribute('media', 'screen, print, handheld, projection'); |
||
| 57 | // pluginCSS.setAttribute('type', 'text/css'); |
||
| 58 | // pluginCSS.appendChild(document.createTextNode(ODFViewerPlugin_css)); |
||
| 59 | // document.head.appendChild(pluginCSS); |
||
| 60 | } |
||
| 61 | |||
| 62 | // that should probably be provided by webodf |
||
| 63 | function nsResolver( prefix ) { |
||
| 64 | var ns = { |
||
| 65 | 'draw': "urn:oasis:names:tc:opendocument:xmlns:drawing:1.0", |
||
| 66 | 'presentation': "urn:oasis:names:tc:opendocument:xmlns:presentation:1.0", |
||
| 67 | 'text': "urn:oasis:names:tc:opendocument:xmlns:text:1.0", |
||
| 68 | 'office': "urn:oasis:names:tc:opendocument:xmlns:office:1.0" |
||
| 69 | }; |
||
| 70 | return ns[prefix] || console.log('prefix [' + prefix + '] unknown.'); |
||
| 71 | } |
||
| 72 | |||
| 73 | var self = this, |
||
| 74 | pluginName = "WebODF", |
||
| 75 | pluginURL = "http://webodf.org", |
||
| 76 | odfCanvas = null, |
||
| 77 | odfElement = null, |
||
| 78 | initialized = false, |
||
| 79 | root = null, |
||
| 80 | documentType = null, |
||
| 81 | pages = [], |
||
| 82 | currentPage = null; |
||
| 83 | |||
| 84 | this.initialize = function ( viewerElement, documentUrl ) { |
||
| 85 | // If the URL has a fragment (#...), try to load the file it represents |
||
| 86 | init(function () { |
||
| 87 | var session, |
||
| 88 | sessionController, |
||
| 89 | sessionView, |
||
| 90 | odtDocument, |
||
| 91 | shadowCursor, |
||
| 92 | selectionViewManager, |
||
| 93 | caretManager, |
||
| 94 | localMemberId = 'localuser', |
||
| 95 | hyperlinkTooltipView, |
||
| 96 | eventManager; |
||
| 97 | |||
| 98 | odfElement = document.getElementById('canvas'); |
||
| 99 | odfCanvas = new odf.OdfCanvas(odfElement); |
||
| 100 | odfCanvas.load(documentUrl); |
||
| 101 | |||
| 102 | odfCanvas.addListener('statereadychange', function () { |
||
| 103 | root = odfCanvas.odfContainer().rootElement; |
||
| 104 | initialized = true; |
||
| 105 | documentType = odfCanvas.odfContainer().getDocumentType(root); |
||
| 106 | if ( documentType === 'text' ) { |
||
| 107 | odfCanvas.enableAnnotations(true, false); |
||
| 108 | |||
| 109 | session = new ops.Session(odfCanvas); |
||
| 110 | odtDocument = session.getOdtDocument(); |
||
| 111 | shadowCursor = new gui.ShadowCursor(odtDocument); |
||
| 112 | sessionController = new gui.SessionController(session, localMemberId, shadowCursor, {}); |
||
| 113 | eventManager = sessionController.getEventManager(); |
||
| 114 | caretManager = new gui.CaretManager(sessionController, odfCanvas.getViewport()); |
||
| 115 | selectionViewManager = new gui.SelectionViewManager(gui.SvgSelectionView); |
||
| 116 | sessionView = new gui.SessionView({ |
||
| 117 | caretAvatarsInitiallyVisible: false |
||
| 118 | }, localMemberId, session, sessionController.getSessionConstraints(), caretManager, selectionViewManager); |
||
| 119 | selectionViewManager.registerCursor(shadowCursor); |
||
| 120 | hyperlinkTooltipView = new gui.HyperlinkTooltipView(odfCanvas, |
||
| 121 | sessionController.getHyperlinkClickHandler().getModifier); |
||
| 122 | eventManager.subscribe("mousemove", hyperlinkTooltipView.showTooltip); |
||
| 123 | eventManager.subscribe("mouseout", hyperlinkTooltipView.hideTooltip); |
||
| 124 | |||
| 125 | var op = new ops.OpAddMember(); |
||
| 126 | op.init({ |
||
| 127 | memberid: localMemberId, |
||
| 128 | setProperties: { |
||
| 129 | fillName: runtime.tr("Unknown Author"), |
||
| 130 | color: "blue" |
||
| 131 | } |
||
| 132 | }); |
||
| 133 | session.enqueue([op]); |
||
| 134 | sessionController.insertLocalCursor(); |
||
| 135 | } |
||
| 136 | |||
| 137 | self.onLoad(); |
||
| 138 | }); |
||
| 139 | }); |
||
| 140 | }; |
||
| 141 | |||
| 142 | this.isSlideshow = function () { |
||
| 143 | return documentType === 'presentation'; |
||
| 144 | }; |
||
| 145 | |||
| 146 | this.onLoad = function () { |
||
| 147 | }; |
||
| 148 | |||
| 149 | this.fitToWidth = function ( width ) { |
||
| 150 | odfCanvas.fitToWidth(width); |
||
| 151 | }; |
||
| 152 | |||
| 153 | this.fitToHeight = function ( height ) { |
||
| 154 | odfCanvas.fitToHeight(height); |
||
| 155 | }; |
||
| 156 | |||
| 157 | this.fitToPage = function ( width, height ) { |
||
| 158 | odfCanvas.fitToContainingElement(width, height); |
||
| 159 | }; |
||
| 160 | |||
| 161 | this.fitSmart = function ( width ) { |
||
| 162 | odfCanvas.fitSmart(width); |
||
| 163 | }; |
||
| 164 | |||
| 165 | this.getZoomLevel = function () { |
||
| 166 | return odfCanvas.getZoomLevel(); |
||
| 167 | }; |
||
| 168 | |||
| 169 | this.setZoomLevel = function ( value ) { |
||
| 170 | odfCanvas.setZoomLevel(value); |
||
| 171 | }; |
||
| 172 | |||
| 173 | // return a list of tuples (pagename, pagenode) |
||
| 174 | this.getPages = function () { |
||
| 175 | var pageNodes = Array.prototype.slice.call(root.getElementsByTagNameNS(nsResolver('draw'), 'page')), |
||
| 176 | pages = [], |
||
| 177 | i, |
||
| 178 | tuple; |
||
| 179 | |||
| 180 | for ( i = 0; i < pageNodes.length; i += 1 ) { |
||
| 181 | tuple = [ |
||
| 182 | pageNodes[i].getAttribute('draw:name'), |
||
| 183 | pageNodes[i] |
||
| 184 | ]; |
||
| 185 | pages.push(tuple); |
||
| 186 | } |
||
| 187 | return pages; |
||
| 188 | }; |
||
| 189 | |||
| 190 | this.showPage = function ( n ) { |
||
| 191 | odfCanvas.showPage(n); |
||
| 192 | }; |
||
| 193 | |||
| 194 | this.getPluginName = function () { |
||
| 195 | return pluginName; |
||
| 196 | }; |
||
| 197 | |||
| 198 | this.getPluginVersion = function () { |
||
| 199 | var version; |
||
| 200 | |||
| 201 | if ( String(typeof webodf) !== "undefined" ) { |
||
| 202 | version = webodf.Version; |
||
| 203 | } else { |
||
| 204 | version = "Unknown"; |
||
| 205 | } |
||
| 206 | |||
| 207 | return version; |
||
| 208 | }; |
||
| 209 | |||
| 210 | this.getPluginURL = function () { |
||
| 211 | return pluginURL; |
||
| 212 | }; |
||
| 213 | } |
||
| 214 |